define("zui/suggestion",["zui/utils","zui/array"],function(require,exports,modules){

		 zUtil=require("zui/utils");
		 zArray=require("zui/array");

		var sliderContentTpl="<div class='slider-content'><ul></ul></div>"; 
		var sliderGroupTpl="<li id='{group}'><ul></ul></li>";
		var sliderTpl="<li class='group-item' data-value='{value}'><a href='javascript:;' >{text}</a></li>";
		
		var  words=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

		function zSuggestion(options){
				this.options={
					el:null,
					inputEl:null,
					source:[],
					cache:{},
					items:[],
					groups:[],
					itemclick:function(ev){},
					renderList:function(data){}
				}

				this._init(options);

		}

		zSuggestion.prototype={

					_init:function(options){
							var _self=this;

							if(!options||!options.srcNode) return;
							_self.el=options.srcNode;
							$(_self.el).html("");
							$(_self.el).append(sliderContentTpl);

							if(!options||!options.searchInput) return;
							_self.inputEl=options.searchInput;

							_self.options=$.extend(_self.options,options||{});

							_self._createContainer();
					},
					_createContainer:function(){
							var _self=this;
							_self._groupSource();
							var sliderGroupInnerHtml="";

							//渲染数据分组模板
							zArray.each(_self.options.groups,function(value,index){

								 sliderGroupInnerHtml= zUtil.substitute(sliderGroupTpl,value)
								 var sliderItemHtml="";
								 zArray.each(_self.options.cache[value['group']],function(value,index){
								 		sliderItemHtml+=zUtil.substitute(sliderTpl,value);

								 });

								 $(".slider-content>ul",$(_self.el)).append(sliderGroupInnerHtml);
								 $("#"+value['group']+">ul", $(".slider-content>ul",$(_self.el))).append(sliderItemHtml);
							});
							
							_self._bindEvent();
							sliderNav(_self);
					},
					_bindEvent:function(){
							var _self=this;
							var slider = $(_self.el); 
							_self.groupItemDomList=$("li.group-item",$(_self.el));
							//选择事件
							_self.groupItemDomList.on("click",function(ev){
									$(".slider-content>ul",$(_self.el)).find("li.group-item.selected").removeClass("selected");
									$(this).addClass("selected");
									var domTarget=ev.target;
									var value=$(this).data("value");
									var text=$(this).text();
									if(zUtil.isFunction(_self.options.itemclick)){
										_self.options.itemclick({domTarget:domTarget,value:value,text:text});
										$(".slider-content>ul",slider).removeClass("search-result");
									}

							});

							//输入框事件
							$(_self.inputEl).on("keyup keydown change",function(){
									var queryText=$(this).val();
									_self._filterText(queryText);
							});

							

					},
					_groupSource:function(){
							var _self=this;
							var data=_self.options.source?_self.options.source:[];
							//生成 快捷索引栏分类
							zArray.each(data,function(value,index){
									if(!zArray.contains(value['group'],_self.options.items)){
											_self.options.items.push(value['group']);
											_self.options.groups.push({"group":value['group']});
											_self.options.cache[value['group']]=[];
											_self.options.cache[value['group']].push(value);
									}
									else{
											_self.options.cache[value['group']].push(value);
									}



							});

							//排序
							var newArray1=zArray.filter(words,function(value,index){

									return zArray.contains(value,_self.options.items);

							});

							

							_self.options.items=newArray1;

							var newArray2=[];
							zArray.each(_self.options.items,function(value,index){

									zArray.each(_self.options.groups,function(elem,key){
											if(elem['group']==value){
												newArray2.push(elem);
											}
									});

							});

							_self.options.groups=newArray2;

							//console.log(_self.options.cache);



					},
					_filterText:function(queryText){
							var _self=this;
							var slider = $(_self.el); 
							_self.searchResult=_self.groupItemDomList.length;
							if(queryText==""){
								$('.slider-mask', slider).addClass("hidden");
								$('.slider-nav', slider).removeClass("hidden");
								$(".slider-content>ul",slider).removeClass("search-result");
								zArray.each(_self.groupItemDomList,function(elem,index){

									$(elem).removeClass("hidden");
								});
								return;
							}
							zArray.each(_self.groupItemDomList,function(elem,index){
								
									if($(elem).text().indexOf(queryText)==-1){
										//console.log(queryText)
										//console.log($(elem).text().indexOf(queryText))
										$(elem).addClass("hidden");
										_self.searchResult-=1;
										
									}
									else{
										$(elem).removeClass("hidden");
										_self.searchResult+=1;
									}

							});

							if(_self.searchResult>0){
								$('.slider-mask', slider).addClass("hidden");
								$('.slider-nav', slider).addClass("hidden");
								$(".slider-content>ul",slider).addClass("search-result");
							}
							else{
								$('.slider-mask', slider).removeClass("hidden");
								$('.slider-nav', slider).addClass("hidden");
								$(".slider-content>ul",slider).removeClass("search-result");
							}

					},
					
					reset:function(){
						 var _self=this;
						 var slider=$(_self.el);

						 zArray.each(_self.groupItemDomList,function(elem,index){
						 	  $(elem).removeClass("hidden");
						 	  $('.slider-mask', slider).addClass("hidden");
							  $('.slider-nav', slider).removeClass("hidden");
							  $(_self.inputEl).val("");
						 });

					}
					




		}


		function sliderNav(self) {
			var defaults = { items: ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]};
			var opts = $.extend(defaults, self.options); 
			var o = $.meta ? $.extend({}, opts, $$.data()) : opts; 
			var slider = $(self.el); 
			$(slider).addClass('slider');

			$(slider).append('<div class="slider-nav"><ul></ul></div>');
			$(slider).append('<div class="slider-mask hidden"><span class="slider-no-suggestion">没有搜到相关结果</span></div>');
			for(var i in o.items) $('.slider-nav ul', slider).append("<li><a alt='#"+o.items[i]+"'>"+o.items[i]+"</a></li>");
			
			$('.slider-nav li', slider).click(function(event){
				var target = $("a",$(this)).attr('alt');
				var cOffset = $('.slider-content', slider).offset().top;
				var tOffset = $('.slider-content '+target, slider).offset().top;
				var height = $('.slider-nav', slider).height(); if(o.height) height = o.height;
				var pScroll = (tOffset - cOffset) - height/8;
				$(this).siblings().removeClass('selected');
				$(this).addClass('selected');
				$(window).scrollTop( pScroll);
			});
		}


		return zSuggestion;

});